'''TEAM XKCD's AWESOME GAME.
'''

import sys, os, shutil, optparse

import pyglet
import cocos
from cocos.director import director
from cocos.menu import *
from cocos.actions import *

from gamelib.options import options
from gamelib import party, world, cutscenes, music, ui, menustyle

def main():
    if options.grant_coin:
        p = party.Party.load('save_game')
        p.coins += int(options.grant_coin)
        p.save('save_game')
        return

    if options.grant_xp:
        p = party.Party.load('save_game')
        for character in p.characters:
            character.experience += int(options.grant_xp)
        p.save('save_game')
        return

    if options.fullscreen:
        director.init(fullscreen=True, do_not_scale=True)
    else:
        w, h = map(int, options.win_size.split(','))
        director.init(width=w, height=h, do_not_scale=True)
    if options.god:
        director.show_FPS = True
    pyglet.gl.glDisable(pyglet.gl.GL_DEPTH_TEST)

    # set up my user interface style
    options.style = ui.default.copy()
    width, height = director.get_window_size()
    ws = width / 1024.
    hs = height / 768.
    if ws < hs:
        options.style.text['dpi'] = int(96. * ws)
    elif hs < ws:
        options.style.text['dpi'] = int(96. * hs)

    if options.play:
        p = party.Party.load('save_game')
        from gamelib import level
        director.run(level.Level(p, options.play, 1))
        return

    if options.inventory:
        p = party.Party.load('save_game')
        from gamelib import inventory
        director.run(inventory.PartyInventoryScreen(p))
        return

    if options.shopping:
        p = party.Party.load('save_game')
        from gamelib import shopping
        director.run(cocos.scene.Scene(shopping.ShoppingLayer(p)))
        return

    director.run(cocos.scene.Scene(MainMenu("Threads of Fate")))


class MainMenu(menustyle.MyStyleMenu):
    def on_enter(self):
        super(MainMenu, self).on_enter()

        self.music_player = music.play('music_menu_t.ogg')

        # ugh, menu should DO this
        for child in self.get_children():
            self.remove(child)

        options = [
            MenuItem('New Game', self.new_game),
            MenuItem('Story', self.story),
            MenuItem('Quit', self.on_quit),
        ]
        if os.path.exists('save_game'):
            options.insert(0, MenuItem('Load Game', self.load_game))

        self.create_menu(options)

    def on_exit(self):
        super(MainMenu, self).on_exit()
        # XXX fade volume down and fade menu out
        self.music_player.pause()
        self.music_player = None

    def load_game(self):
        savedir = 'save_game'
        p = party.Party.load(savedir)
        director.push(world.MapScene(savedir, p))
        # XXX director.push(cocos.scene.Scene(LoadMenu('Load Game')))

    def new_game(self):
        savedir = 'save_game'

        if os.path.exists(savedir):
            director.push(cocos.scene.Scene(ConfirmNuke('Destroy existing game?')))
            return

        os.mkdir(savedir)
        p = party.Party.create(savedir)
        director.push(world.MapScene(savedir, p))

    def story(self):
        intro = cocos.scene.Scene(cutscenes.Intro())
        director.push(intro)

    def on_quit(self):
        pyglet.app.exit()


class ConfirmNuke(menustyle.MyStyleMenu):
    def on_enter(self):
        super(ConfirmNuke, self).on_enter()
        self.menu_valign = CENTER
        self.menu_halign = CENTER

        self.font_item['color'] = (255, 255, 255, 255)
        self.font_item_selected['color'] = (200, 200, 255, 255)
        self.font_title['color'] = (255, 255, 255, 255)

        # ugh, menu should DO this
        for child in self.get_children():
            self.remove(child)

        options = [
            MenuItem("Don't do it", self.on_no),
            MenuItem('Destroy old save game', self.on_yes),
        ]

        self.create_menu(options)

    def on_no(self):
        self.parent.end()

    def on_yes(self):
        savedir = 'save_game'
        shutil.rmtree(savedir)
        os.mkdir(savedir)

        p = party.Party.create(savedir)
        director.replace(world.MapScene(savedir, p))

